package com.papers.juan.controller;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.papers.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.annotation.Log;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.core.validate.QueryGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.vo.TJuanQuestionReposVo;
import com.papers.juan.domain.bo.TJuanQuestionReposBo;
import com.papers.juan.service.ITJuanQuestionReposService;
import com.papers.common.core.page.TableDataInfo;

/**
 * 题库
 *
 * @author papers
 * @date 2022-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/juan/juanQuestionRepos")
public class TJuanQuestionReposController extends BaseController {

    private final ITJuanQuestionReposService iTJuanQuestionReposService;

    /**
     * 查询题库列表
     */
    @SaCheckPermission("juan:juanQuestionRepos:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanQuestionReposVo> list(TJuanQuestionReposBo bo, PageQuery pageQuery) {
        bo.setCreatedBy(LoginHelper.getUserId() + "");
        return iTJuanQuestionReposService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库列表
     */
    @SaCheckPermission("juan:juanQuestionRepos:export")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanQuestionReposBo bo, HttpServletResponse response) {
        List<TJuanQuestionReposVo> list = iTJuanQuestionReposService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库", TJuanQuestionReposVo.class, response);
    }

    /**
     * 获取题库详细信息
     *
     * @param reposId 主键
     */
    @SaCheckPermission("juan:juanQuestionRepos:query")
    @GetMapping("/{reposId}")
    public R<TJuanQuestionReposVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String reposId) {
        return R.ok(iTJuanQuestionReposService.queryById(reposId));
    }

    /**
     * 新增题库
     */
    @SaCheckPermission("juan:juanQuestionRepos:add")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanQuestionReposBo bo) {
        return iTJuanQuestionReposService.addRepos(bo);
    }

    /**
     * 修改题库
     */
    @SaCheckPermission("juan:juanQuestionRepos:edit")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanQuestionReposBo bo) {
        return iTJuanQuestionReposService.editRepos(bo);
    }

    /**
     * 删除题库
     *
     * @param reposIds 主键串
     */
    @SaCheckPermission("juan:juanQuestionRepos:remove")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reposIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] reposIds) {
        return toAjax(iTJuanQuestionReposService.deleteWithValidByIds(Arrays.asList(reposIds), true) ? 1 : 0);
    }

    /**
     * 题库统计图
     * @return
     */
    @GetMapping("/getReposChart")
    public R getReposChart() {
        return iTJuanQuestionReposService.getReposChart();
    }


    /**
     * 合并题库
     * @return
     */
    @SaCheckPermission("juan:juanQuestionRepos:add")
    @RepeatSubmit()
    @PostMapping("/mergeRepos")
    public R mergeRepos(@RequestBody TJuanQuestionReposBo bo) {

        List<String> idList = Arrays.asList(bo.getIds());
        return toAjax(iTJuanQuestionReposService.mergeRepos(bo,idList));

    }
}
